package com.example.service.impl;

import java.util.Optional;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.example.common.HttpStatusConstants;
import com.example.common.R;
import com.example.mapper.AppointmentMapper;
import com.example.mapper.CommentMapper;
import com.example.mapper.ConUserCommentMapper;
import com.example.mapper.EmployeeScoreMapper;
import com.example.mapper.OrderMapper;
import com.example.mapper.UserMapper;
import com.example.model.AddCommentDTO;
import com.example.model.AddUserCommentDTO;
import com.example.model.req.AddCommentReq;
import com.example.service.CommentService;
import com.example.utils.JwtUtil;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private ConUserCommentMapper conUserCommentMapper;
    @Autowired
    private EmployeeScoreMapper employeeScoreMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private AppointmentMapper appointmentMapper;

    @Override
    @Transactional
    public R<String> addComment(AddCommentReq addCommentReq, HttpServletRequest request) {
        try {
            String openId = JwtUtil.getOpenIdFromToken(request.getHeader("Authorization").replace("Bearer ", ""));
            Integer userId = userMapper.getIdByOpenId(openId);

            AddCommentDTO addCommentDTO = AddCommentDTO.builder()
                    .comment(addCommentReq.getComment()).build();
            commentMapper.addComment(addCommentDTO);

            AddUserCommentDTO addUserCommentDTO = AddUserCommentDTO.builder()
                    .userId(userId)
                    .commentId(addCommentDTO.getId())
                    .build();
            conUserCommentMapper.addUserComment(addUserCommentDTO);

            Integer worker = addCommentReq.getWorker();
            Integer score = addCommentReq.getScore();
            employeeScoreMapper.addEmployeeScore(worker, score, addCommentDTO.getId());

            if (addCommentReq.getOrderType()) {
                orderMapper.updateCommented(addCommentReq.getOrderId());
            } else {
                appointmentMapper.updateCommented(addCommentReq.getOrderId());
            }

            return R.success();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(HttpStatusConstants.INTERNAL_SERVER_ERROR, "请稍后重试");
        }
    }

    @Override
    public R<Boolean> hasCommented(Integer orderId, boolean orderType) {
        try {
            Boolean res = null;
            if (orderType) {
                res = Optional.ofNullable(orderMapper.hasCommented(orderId))
                        .orElse(false);
            } else {
                boolean hasCommented = appointmentMapper.hasCommented(orderId);
                res = Optional.ofNullable(hasCommented).orElse(false);
            }

            return R.success(res);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(HttpStatusConstants.INTERNAL_SERVER_ERROR, "");
        }
    }
}
